package cn.holy.app.xcddt.ui.activity.main

import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.repository.UserRepository
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_main.*
import javax.inject.Inject

/**
 * Created by xiong on 18-2-23.
 */
@Route(path = "/app/main")
class MainActivity : BaseActivity() {

    @Inject
    lateinit var userRepo: UserRepository

    override fun initData() {

        App.userInfo?.let {
            if (it.phone == "") {
                QMUIDialog.MessageDialogBuilder(this@MainActivity)
                        .setMessage("检测到您未设置手机，请立即绑定，否则将不能上传案卷信息.")
                        .setTitle("提示")
                        .addAction("立即绑定") { dialog, _ ->
                            ARouter.getInstance().build("/app/setting/bind").navigation()
                            dialog.dismiss()
                        }.show()
            }
            tv_nickname.text = it.username
        }


    }


    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle(R.string.app_name).setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_close, R.id.action_close).setOnClickListener({ finish() })
        val addRightTextButton = top_bar.addRightTextButton("设置", R.id.action_settings)
        addRightTextButton.setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        addRightTextButton.setOnClickListener {
            ARouter.getInstance().build("/app/setting").navigation()
        }
        //导航
        btn_photo.setOnClickListener {
                        checkPermission()

//            var time = 5
//            var countTime = time
//            Observable.interval(0, 1, TimeUnit.SECONDS)
//                    .subscribeOn(Schedulers.newThread())
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .map { long -> return@map  long.toInt() }
//                    .take(countTime)
//                    .subscribe({ count ->
//                        Logger.d(count)
//                        qmuiProgressBar.progress = count
//                    }, {}, {
//                        Logger.d("倒计时完成")
//                    })
        }
        btn_action.setOnClickListener { ARouter.getInstance().build("/app/manager").navigation() }
        btn_check.setOnClickListener {
            ARouter.getInstance().build("/app/archives").navigation()
        }
        btn_logout.setOnClickListener {
            userRepo.logoutUser()
            App.sharedPreferences.edit {
                putString("token", "")
            }
            ARouter.getInstance().build("/app/login").navigation()
            finish()
        }
        val packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES)
        val versionCode = packageInfo.versionCode
        val versionName = packageInfo.versionName
        tv_version.text = "ver. $versionName($versionCode)"

    }

    private fun checkPermission() {
        AndPermission.with(this)
                .runtime()
                .permission(
                        Permission.Group.CAMERA,
                        Permission.Group.LOCATION,
                        Permission.Group.MICROPHONE,
                        Permission.Group.STORAGE)
                .onGranted {
                    ARouter.getInstance().build("/app/record/state").navigation()
                }.onDenied {

                }.start()
    }

    override fun initLayout() {
        setContentView(R.layout.activity_main)
    }


}
